// import * as constants from './constants'
// Immutable对象不可被变更
// fromJS可以帮我们把JS对象转换成Immutable对象
import {fromJS} from "immutable";
import * as constants from './constants';

const defaultState = fromJS({
    topicList: [],
    articleList: [],
    articlePage: 1,
    recommendList: [],
    QrCodeMouse: false,
    writerList: [],
    writerPage: 1,
    writerTotalPage: 1,
    showScroll: false,
    top: false

});
const changeHomeData=(state,action)=>{
    return state.merge({
        topicList: fromJS(action.topicList),
        articleList: fromJS(action.articleList),
        recommendList: fromJS(action.recommendList)
    });
};
const addArticleList=(state,action)=>{
    return state.merge({
        'articleList': state.get('articleList').concat(action.list),
        'articlePage': action.nextPage
    });
};
const changeWriterList=(state,action)=>{
    return state.merge({
        writerList: action.data,
        writerTotalPage: action.totalPage
    });
};
export default (state = defaultState, action) => {
    switch (action.type) {
        case constants.MOUSE_ENTER:
            return state.set('QrCodeMouse', true);
        case constants.MOUSE_LEAVE:
            return state.set('QrCodeMouse', false);
        case constants.HANDLE_MOUSE_ENTER:
            return state.set('top', true);
        case constants.HANDLE_MOUSE_LEAVE:
            return state.set('top', false);
        case constants.BIND_HANDLE_SCROLL:
            return state.set('showScroll', action.state);
        case constants.CHANGE_HOME_DATA:
            return changeHomeData(state,action);
        case constants.WRITER_INFO_SWITCH:
            return state.set('writerPage', action.page);
        case constants.ADD_ARTICLE_LIST:
            //immutable中concat方法是在immutable后面添加数据
            // return state.set('articleList',state.get('articleList').concat(action.list));
            return addArticleList(state,action);
        case constants.CHANGE_WRITER_LIST:
            return changeWriterList(state,action);
        default:
            return state;
    }
}
